package com.hyjx.framework.uc.user;

import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.common.util.StringHelper;
import com.hyjx.framework.component.field.ExtFieldService;
import com.hyjx.framework.orm.dao.IBaseDao;
import com.hyjx.framework.orm.dao.Pager;
import com.hyjx.framework.service.RightManager;
import com.hyjx.framework.service.UserCenterService;
import com.hyjx.framework.uc.depart.DepartModel;
import com.hyjx.framework.uc.menu.MenuModel;
import com.hyjx.framework.uc.role.RoleModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户 服务实现类
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private IBaseDao dao;
    
    @Resource
    private ExtFieldService fieldService;
    
    
    /**
     * 添加数据对象
     *
     * @param model
     */
    @Override
    public boolean add(UserModel model) {
        dao.addObject(model.getModuleName() + ".insert", model);
        saveRelation(model.getUser_id(), model.getRoleIds());
        
        fieldService.update(model.getExtData());
        return true;
    }
    
    /**
     * 修改数据对象
     *
     * @param model
     */
    @Override
    public int update(UserModel model) {
        dao.deleteObject(model.getModuleName() + ".deleteUserRole", model.getUser_id());
        dao.updateObject(model.getModuleName() + ".update", model);
        saveRelation(model.getUser_id(), model.getRoleIds());
        
        fieldService.update(model.getExtData());
        return 1;
    }
    
    /**
     * 保存对应关系
     *
     * @param userId
     * @param roleIds
     */
    private void saveRelation(String userId, String roleIds) {
        if (roleIds != null && !"".equals(roleIds)) {
            String[] id = roleIds.split(",");
            List<UserRoleModel> list = new ArrayList<UserRoleModel>();
            for (int i = 0; i < id.length; i++) {
                String menuId = id[i];
                if (menuId != null && !"".equals(menuId)) {
                    UserRoleModel rm = new UserRoleModel();
                    
                    rm.setRole_id(menuId);
                    rm.setUser_id(userId);
                    rm.setId(StringHelper.getPrimaryKey());
                    list.add(rm);
                }
            }
            dao.batchUpdateObject(new UserModel().getModuleName() + ".insertUserRole", list);
        }
    }
    
    /**
     * 删除数据对象
     *
     * @param id
     */
    @Override
    public int delete(String id) {
        dao.deleteObject("user.deleteUserRole", id);
        return dao.deleteObject("user.delete", id);
    }
    
    /**
     * 获取数据对象
     */
    @Override
    public String getUsers(UserBean bean) {
        UserModel userModel = new UserModel();

        
        bean.setUserIsAdmin(UserCenterService.getCurrentUserInfo().getUserIsAdmin());
        Pager pager = dao.getPageList(userModel.getModuleName() + ".getusers", userModel.getModuleName() + ".getusersNum", bean);
        
        for (UserModel model : (List<UserModel>) pager.getResultList()) {
            String departCode = model.getDepart_code();
            if (departCode != null && !"".equals(departCode)) {
                DepartModel depart = RightManager.getDepartByCode(departCode);
                if (depart != null) {
                    model.setDepartName(depart.getDepart_name());
                }
            }
        }
        
        return CommonUtil.getPageJsonStr(pager);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public String getUserRoleStr(String id) {
        List<UserRoleModel> userList = null;
        userList = dao.queryObjectList(new UserModel().getModuleName() + ".queryUserRoleList", id);
        
        String returnStr = "";
        if (userList.size() > 0) {
            for (UserRoleModel m : userList) {
                returnStr += "," + m.getRole_id();
            }
            return returnStr.substring(1);
        } else {
            return returnStr;
        }
    }
    
    @Override
    public UserModel getUserById(String s) {
        UserModel model = null;
        model = (UserModel) dao.queryObject(new UserModel().getModuleName() + ".getUserById", s);
        return model;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<DepartModel> getUser4Tree() throws SQLException {
        return dao.queryObjectList(new UserModel().getModuleName() + ".getuser4tree", null);
    }
    
    @Override
    public UserModel login(UserModel userModel) throws SQLException {
        @SuppressWarnings("unchecked")
        List<UserModel> list = dao.queryObjectList(new UserModel().getModuleName() + ".login", userModel);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<MenuModel> getUserMenuList(String user_id) throws SQLException {
        return dao.queryObjectList(new MenuModel().getModuleName() + ".getUserMenuList", user_id);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<RoleModel> getuserRoleList(String user_id) throws SQLException {
        return dao.queryObjectList(new RoleModel().getModuleName() + ".getuserRoleList", user_id);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<UserModel> getUsersByDeptCode(String depart_Code) throws SQLException {
        return dao.queryObjectList(new UserModel().getModuleName() + ".getUsersByDeptCode", depart_Code);
    }
    
    @Override
    public int updatepwd(UserModel model) {
        return dao.updateObject(model.getModuleName() + ".updatepwd", model);
    }
    
    @Override
    public List<MenuModel> getNavMenuList(List<MenuModel> list) {
        List<MenuModel> result = new ArrayList<MenuModel>();
        
        for (MenuModel model : list) {
            if (null == model.getMenu_parent_code()) {
                result.add(model);
            }
        }
        return result;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<UserModel> getAllUsers() {
        return dao.queryObjectList(new UserModel().getModuleName() + ".getallusers");
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<UserRoleModel> queryAllUserRoleList() {
        return dao.queryObjectList(new UserModel().getModuleName() + ".queryAllUserRoleList");
    }
    
    @Override
    public boolean disableUserByDeptCode(String deptCode) {
        dao.updateObject(new UserModel().getModuleName() + ".disableUserByDeptCode", deptCode);
        return true;
    }
    
    @Override
    public int logic_delete(String ids) {
        return dao.updateObject(new UserModel().getModuleName() + ".logic_delete", ids);
    }
}